#coding=utf-8
from driver import Proxy
import random  # 取随机数
import re
from db.mysql_conn import MysqlUtils, config
mysql = MysqlUtils(config)
from config.conf import proxiesDEF

get_goods_num = 0
goodsArr = []

def getAsin():
    try:
        goodsArr = mysql.query('select id, dp, star, reviews, url, name, img, country from newbook where price = 0 limit 50')
    except:
        goodsArr = []

    proxies_list = ['221.122.91.60:80', '125.123.127.251:9000', '125.73.220.18:49128', '106.12.201.174:8080', '125.123.124.192:9000', '125.123.126.137:9000', '125.123.65.5:9000', '27.195.216.21:8118', '222.128.9.235:33428', '125.123.121.214:9000', '114.249.113.151:9000', '123.56.74.221:80', '223.156.112.100:9000', '47.104.172.108:8118', '125.123.124.145:9000', '221.122.91.65:80', '122.136.212.132:53281', '125.123.44.147:9000', '218.108.175.15:80', '52.83.202.3:8118', '47.107.158.219:8000', '124.205.155.157:9090', '182.35.81.10:9999', '45.125.32.181:3128', '182.35.86.175:9999', '1.198.72.162:9999', '163.204.242.30:9999', '125.123.44.81:9000', '61.160.210.223:808', '119.178.101.18:8888', '221.122.91.61:80', '123.132.232.254:61017', '114.225.29.112:8118', '43.254.168.56:53281', '69.160.173.3:8000', '123.163.97.154:9999', '163.204.244.166:9999', '163.204.243.219:9999', '163.204.245.109:9999', '125.123.66.161:9000', '134.175.55.15:8888', '182.35.80.251:9999', '125.123.141.95:9000', '163.125.70.239:8888', '218.60.8.83:3129', '125.65.79.60:3311', '116.52.100.233:8888', '182.122.205.72:8118', '106.9.169.41:808', '175.43.34.17:9999']
    # boo = [True, False, True, False, False, False, False, False, False]
    boo = [False]
    try:
        if random.choice(boo):
            proxies_list = proxiesDEF()
            print(proxies_list)
    except:
        print('获取ip失败')

    ip = proxies_list[random.randint(0, len(proxies_list)-1)]
    driver = Proxy(ip).getdriver()

    if len(goodsArr) > 0:
        getGoodsDeDetail(driver, goodsArr)
    else:
        driver.quit()
        print('=========over==============')


def getGoodsDeDetail (driver, goodsArr):
    global get_goods_num
    for index, goods in enumerate(goodsArr):
        print('==%s==================%s=================================' % (goods['id'], get_goods_num))
        get_goods_num = get_goods_num + 1
        url = 'https://www.amazon.com/dp/' + goods['dp']
        # url = 'https://www.amazon.com/dp/0062301675'
        print(url)
        try:
            driver.get(url)
            driver.implicitly_wait(random.uniform(2.5, 4.3))
            try:
                name = driver.find_element_by_xpath('//*[@id="productTitle"]').text
            except:
                name = "xxxxxxxxx"
            try:
                resArr = driver.find_element_by_xpath('//li[@id="SalesRank"]').text
                restr = re.compile('Rank: #(.*) \(See Top')
                rankarr = restr.findall(resArr)[0].split(' in ')
                ranking = rankarr[0].replace(',', '')
                rankingtype = rankarr[1]
            except:
                ranking = -1
                rankingtype = ''
            try:
                ISBN10 = driver.find_element_by_xpath('//div/span[contains(text(), "ISBN-10:")]/following-sibling::span[1]').text
            except:
                ISBN10 = ''
            try:
                ISBN13 = driver.find_element_by_xpath('//div/span[contains(text(), "ISBN-13:")]/following-sibling::span[1]').text
            except:
                ISBN13 = ''
            try:
                Paperback = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Paperback")]/../following-sibling::div[1]/span').text
            except:
                Paperback = ''
            try:
                Hardcover = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Hardcover")]/../following-sibling::div[1]/span').text
            except:
                Hardcover = ''
            try:
                Kindle = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Kindle")]/../following-sibling::div[1]/span').text
            except:
                Kindle = ''
            try:
                Audiobook = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audiobook")]/../following-sibling::div[1]/span').text
            except:
                Audiobook = ''
            try:
                AudioCD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Audio CD")]/../following-sibling::div[1]/span').text
            except:
                AudioCD = ''
            try:
                MP3CD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "MP3 CD")]/../following-sibling::div[1]/span').text
            except:
                MP3CD = ''
            try:
                SpiralBound = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Spiral-bound")]/../following-sibling::div[1]/span').text
            except:
                SpiralBound = ''
            try:
                eTextbook = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "eTextbook")]/../following-sibling::div[1]/span').text
            except:
                eTextbook = ''
            try:
                LooseLeaf = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Loose Leaf")]/../following-sibling::div[1]/span').text
            except:
                LooseLeaf = ''
            try:
                PlasticComb = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Plastic Comb")]/../following-sibling::div[1]/span').text
            except:
                PlasticComb = ''
            try:
                PrintedAccessCode = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Printed Access Code")]/../following-sibling::div[1]/span').text
            except:
                PrintedAccessCode = ''
            try:
                UnknownBinding = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Unknown Binding")]/../following-sibling::div[1]/span').text
            except:
                UnknownBinding = ''
            try:
                BookSupplement = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Book Supplement")]/../following-sibling::div[1]/span').text
            except:
                BookSupplement = ''
            try:
                MultimediaCD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Multimedia CD")]/../following-sibling::div[1]/span').text
            except:
                MultimediaCD = ''
            try:
                Ringbound = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Ring-bound")]/../following-sibling::div[1]/span').text
            except:
                Ringbound = ''
            try:
                Pamphlet = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Pamphlet")]/../following-sibling::div[1]/span').text
            except:
                Pamphlet = ''
            try:
                LibraryBinding = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "Library Binding")]/../following-sibling::div[1]/span').text
            except:
                LibraryBinding = ''
            try:
                DVDROM = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD-ROM")]/../following-sibling::div[1]/span').text
            except:
                DVDROM = ''
            try:
                DVD = driver.find_element_by_xpath('//div[@class="a-row"]/span[contains(text(), "DVD")]/../following-sibling::div[1]/span').text
            except:
                DVD = ''

            goods['ranking'] = ranking
            goods['rankingtype'] = rankingtype
            goods['ISBN10'] = ISBN10
            goods['ISBN13'] = ISBN13
            goods['asin'] = goods['dp']

            print(name)
            print(ranking)
            print(rankingtype)
            print(ISBN10)
            print(ISBN13)
            # print(Paperback)
            # print(Hardcover)
            # print(Kindle)
            # print(AudioCD)
            # print(MP3CD)
            # print(Audiobook)
            # print(SpiralBound)
            # print(eTextbook)
            # print(PrintedAccessCode)
            # print(PlasticComb)
            # print(UnknownBinding)
            print(Paperback)
            print(Hardcover)
            print(Kindle)
            print(AudioCD)
            print(MP3CD)
            print(Audiobook)
            print(SpiralBound)
            print(eTextbook)
            print(PlasticComb)
            print('Paperback ====   %s   ==' % Paperback)
            print('Hardcover ====   %s   ==' % Hardcover)
            print('Kindle =======   %s   ==' % Kindle)
            print('AudioCD ======   %s   ==' % AudioCD)
            print('MP3CD ======   %s   ==' % MP3CD)
            print('Audiobook ====   %s   ==' % Audiobook)
            print('SpiralBound ====   %s   ==' % SpiralBound)
            print('eTextbook ====   %s   ==' % eTextbook)
            print('Loose Leaf ====   %s   ==' % LooseLeaf)
            print('PlasticComb ====   %s   ==' % PlasticComb)
            print('PrintedAccessCode ====   %s   ==' % PrintedAccessCode)
            print('UnknownBinding ====   %s   ==' % UnknownBinding)
            print('BookSupplement ====   %s   ==' % BookSupplement)
            print('MultimediaCD ====   %s   ==' % MultimediaCD)
            print('Ringbound ====   %s   ==' % Ringbound)
            print('Pamphlet ====   %s   ==' % Pamphlet)
            print('LibraryBinding ====   %s   ==' % LibraryBinding)
            print('DVDROM ====   %s   ==' % DVDROM)
            print('DVD ====   %s   ==' % DVD)

            sqlStr = "INSERT INTO newbook (dp,packagetype,packagelink,ISBN10,ISBN13,asin,rankingtype,ranking,star,reviews,moneytype,price,url,name,img,country ) values (%s, %s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s)"

            if Paperback != '':
                try:
                    goods['packagetype'] = 'Paperback'
                    Paperback = Paperback.replace('from', '').strip()
                    moneytype = Paperback[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Paperback.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert paperback')
                except:
                    print('Paperback=== error')
            if Hardcover != '':
                try:
                    goods['packagetype'] = 'Hardcover'
                    Hardcover = Hardcover.replace('from', '').strip()
                    moneytype = Hardcover[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Hardcover.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Hardcover')
                except:
                    print('Hardcover=== error')
            if Kindle != '':
                try:
                    goods['packagetype'] = 'Kindle'
                    Kindle = Kindle.replace('from', '').strip()
                    moneytype = Kindle[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Kindle.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Kindle')
                except:
                    print('Kindle=== error')

            if AudioCD != '':
                try:
                    goods['packagetype'] = 'AudioCD'
                    AudioCD = AudioCD.replace('from', '').strip()
                    moneytype = AudioCD[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(AudioCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert AudioCD')
                except:
                    print('AudioCD=== error')
            if MP3CD != '':
                try:
                    goods['packagetype'] = 'MP3CD'
                    MP3CD = MP3CD.replace('from', '').strip()
                    moneytype = MP3CD[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MP3CD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MP3CD')
                except:
                    print('MP3CD=== error')
            if Audiobook != '':
                try:
                    goods['packagetype'] = 'Audiobook'
                    Audiobook = Audiobook.replace('from', '').strip()
                    moneytype = Audiobook[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Audiobook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Audiobook')
                except:
                    print('Audiobook=== error')

            if SpiralBound != '':
                try:
                    goods['packagetype'] = 'SpiralBound'
                    SpiralBound = SpiralBound.replace('from', '').strip()
                    moneytype = SpiralBound[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(SpiralBound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert SpiralBound')
                except:
                    print('SpiralBound=== error')
            if eTextbook != '':
                try:
                    goods['packagetype'] = 'eTextbook'
                    eTextbook = eTextbook.replace('from', '').strip()
                    moneytype = eTextbook[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(eTextbook.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert eTextbook')
                except:
                    print('eTextbook=== error')
            if LooseLeaf != '':
                try:
                    goods['packagetype'] = 'LooseLeaf'
                    LooseLeaf = LooseLeaf.replace('from', '').strip()
                    moneytype = LooseLeaf[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LooseLeaf.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LooseLeaf')
                except:
                    print('LooseLeaf=== error')
            if PlasticComb != '':
                try:
                    goods['packagetype'] = 'PlasticComb'
                    PlasticComb = PlasticComb.replace('from', '').strip()
                    moneytype = PlasticComb[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PlasticComb.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PlasticComb')
                except:
                    print('PlasticComb=== error')
            if PrintedAccessCode != '':
                try:
                    goods['packagetype'] = 'PrintedAccessCode'
                    PrintedAccessCode = PrintedAccessCode.replace('from', '').strip()
                    moneytype = PrintedAccessCode[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(PrintedAccessCode.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert PrintedAccessCode')
                except:
                    print('PrintedAccessCode=== error')
            if UnknownBinding != '':
                try:
                    goods['packagetype'] = 'UnknownBinding'
                    UnknownBinding = UnknownBinding.replace('from', '').strip()
                    moneytype = UnknownBinding[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(UnknownBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert UnknownBinding')
                except:
                    print('UnknownBinding=== error')
            if BookSupplement != '':
                try:
                    goods['packagetype'] = 'BookSupplement'
                    BookSupplement = BookSupplement.replace('from', '').strip()
                    moneytype = BookSupplement[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(BookSupplement.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert BookSupplement')
                except:
                    print('BookSupplement=== error')
            if MultimediaCD != '':
                try:
                    goods['packagetype'] = 'MultimediaCD'
                    MultimediaCD = MultimediaCD.replace('from', '').strip()
                    moneytype = MultimediaCD[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(MultimediaCD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert MultimediaCD')
                except:
                    print('MultimediaCD=== error')
            if Ringbound != '':
                try:
                    goods['packagetype'] = 'Ringbound'
                    Ringbound = Ringbound.replace('from', '').strip()
                    moneytype = Ringbound[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Ringbound.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Ringbound')
                except:
                    print('Ringbound=== error')
            if Pamphlet != '':
                try:
                    goods['packagetype'] = 'Pamphlet'
                    Pamphlet = Pamphlet.replace('from', '').strip()
                    moneytype = Pamphlet[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(Pamphlet.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert Pamphlet')
                except:
                    print('Pamphlet=== error')
            if LibraryBinding != '':
                try:
                    goods['packagetype'] = 'LibraryBinding'
                    LibraryBinding = LibraryBinding.replace('from', '').strip()
                    moneytype = LibraryBinding[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(LibraryBinding.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert LibraryBinding')
                except:
                    print('LibraryBinding=== error')
            if DVDROM != '':
                try:
                    goods['packagetype'] = 'DVDROM'
                    DVDROM = DVDROM.replace('from', '').strip()
                    moneytype = DVDROM[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVDROM.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVDROM')
                except:
                    print('DVDROM=== error')
            if DVD != '':
                try:
                    goods['packagetype'] = 'DVD'
                    DVD = DVD.replace('from', '').strip()
                    moneytype = DVD[0:1]
                    goods['moneytype'] = moneytype
                    goods['price'] = averageNum(DVD.replace(moneytype, ''))
                    sqlParam = returnGoods(goods)
                    mysql.insert(sqlStr, sqlParam)
                    print('insert DVD')
                except:
                    print('DVD=== error')
            if Paperback != '' or Hardcover != '' or Kindle != '' or AudioCD != '' or MP3CD != '' or Audiobook != '' or SpiralBound != '' or eTextbook != '' or LooseLeaf != '' or PlasticComb != '' or PrintedAccessCode != '' or UnknownBinding != '' or BookSupplement != '' or MultimediaCD != '' or Ringbound != '' or Pamphlet != '' or LibraryBinding != '' or DVDROM != '' or DVD != '':
                try:
                    deletesql = "DELETE FROM newbook WHERE id =  %s" % (goods['id'])
                    mysql.delete(deletesql)
                except:
                    print('deletesql   === error')

            # 清空数组 goods
            print('*goods***********************************')
            goods.clear()
            print(goods)

        except Exception as exc:
            print(exc)
            print('get 请求失败 跳往下一个')
    driver.quit()
    print('休息一会==========马上开始')
    getAsin()


def updateSql(goods):
    d = {
        'packagetype': goods['packagetype'],
        'ISBN10': goods['ISBN10'],
        'ISBN13': goods['ISBN13'],
        'asin': goods['asin'],
        'rankingtype': goods['rankingtype'],
        'ranking': goods['ranking'],
        'moneytype': goods['moneytype'],
        'price': goods['price'],
         }  # 更新的字段及对应值
    t = ('id', goods['id'])  # 更新所需条件
    table_name = 'newbook'
    updateSqlStr = 'UPDATE %s SET ' % table_name + ','.join(['%s=%r' % (k, d[k]) for k in d]) + ',updateTime = NOW() WHERE %s=%r;' % (t[0], t[1])
    mysql.update(updateSqlStr)


def returnGoods(goods):
    sqlParam = [
        goods['dp'],
        goods['packagetype'],
        '',
        goods['ISBN10'],
        goods['ISBN13'],
        goods['asin'],
        goods['rankingtype'],
        goods['ranking'],
        goods['star'],
        goods['reviews'],
        goods['moneytype'],
        goods['price'],
        goods['url'],
        goods['name'],
        goods['img'],
        goods['country']
    ]
    return sqlParam

def averageNum(str):
    try:
        if ' - ' in str:
            arr = str.split(' - ')
            add = float(arr[0]) + float(arr[1])
            price = '%.2f' % float(add/2)
        else:
            price = '%.2f' % float(str)
    except:
        price = -1
    return price

if __name__ == '__main__':
    getAsin()
